// pages/passengers/focusRegularBus/focusRegularBus.js
var Tool = require("../../../utils/ztool.js");
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IDD: '',
    focusList: [],
    driverlist: [],
    list: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var idd = options.idd
    console.log(idd)
    if (idd) {
      this.data.IDD = idd
      console.log(this.data.IDD)
      this.loadData()
      // this.getdriverandcar()
      this.loadOrderData()

      this.newGetDriverAndCar()
    } else {
      console.log('没有idd不跳转到detail')
    }
    var password = wx.getStorageSync('password')
    console.log(password)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  //新的获取车辆和订单的接口
  newGetDriverAndCar: function () {
    var that = this
    var userInfo = wx.getStorageSync('newuserInfo');
    console.log(userInfo)
    that.setData({
      driverlist: [],
    });
    console.log(that.data.driverlist.length)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
      data: {
        idd: that.data.IDD,
        tel: userInfo.cTel,
        pwd: userInfo.UserPwd,
        act: 'atn_status'
      },
      method: 'GET',
      success: function (res) {

        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
        }

        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {

          // if (data.data[0].bcTime != '') {
          //   for (var i = 0; i < data.data.length; i++) {
          //     data.data[i].bcTime = data.data[i].bcTime.substring(5, 16)
          //   }
          // }

          that.setData({
            driverlist: data.data
          });

          console.log(that.data.driverlist)
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  //获取班车的信息
  loadData: function () {
    var that = this
    var userInfo = wx.getStorageSync('newuserInfo');
    that.data.focusList = []
    console.log(userInfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
      data: {
        tel: userInfo.cTel,
        pwd: userInfo.UserPwd,
        act: 'getcgslines',
        idd: that.data.IDD
        // idd: '2018-03-26{b1448a16-4d0c-4d75-a470-02b6ecee5f89}'
      },
      method: 'GET',
      success: function (res) {
        var data = res.data
        console.log(data)
        if (data.code == '0') {
          console.log('接口调用成功，code=0')
          if (data.data.length > 0) {
            console.log('有订单数据')
            for (var i = 0; i < data.data.length; i++) {
              data.data[i].sTime = data.data[i].sTime.substring(11, 16)
            }
            that.setData({
              focusList: data.data,
            });
            console.log(that.data.focusList)
          }
        } else {
          //返回code不为0，说明有错误
          console.log('接口调用成功，但是操作有误，返回值不为0，打印错误信息')
        }
      },
      fail: function () {
        //接口加载失败
        console.log('加载接口错误');
      }
    })
  },

  focus: function (e) {
    var that = this
    console.log(e)
    var bctype = e.currentTarget.dataset.bctype
    var bcname = ''
    var ldnum = ''
    var lname = ''
    switch (bctype) {
      case '0':
        bcname = e.currentTarget.dataset.bcname
        ldnum = e.currentTarget.dataset.ldnum
        lname = e.currentTarget.dataset.lname
        console.log(ldnum)
        console.log(lname)
        break;
      case '1':
        bcname = e.currentTarget.dataset.carname
        break;
      case '2':
        bcname = that.data.IDD
        break;
    }
    var userInfo = wx.getStorageSync('newuserInfo');
    console.log(userInfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
      data: {
        tel: userInfo.cTel,
        pwd: userInfo.UserPwd,
        act: 'atn_add',
        ycid: that.data.list.WebCompanyID,
        yctel: that.data.list.AgencyTel,
        ycname: that.data.list.WCompanyName,
        gcid: that.data.list.CarCompanyID,
        gctel: '',
        gcname: '',
        lname: lname,
        bcname: bcname,
        bctype: bctype,
        ldnum: ldnum,
      },
      method: 'GET',
      success: function (res) {
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          return
        }
        var data = tool.getRegularJson(res.data);
        console.log(data)
        if (data.code == '0') {
          wx.showToast({
            title: '关注成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })

          that.loadData()
          that.loadOrderData()
          that.newGetDriverAndCar()
          // that.getdriverandcar()
        } else {
          wx.showToast({
            title: '关注失败，请重试',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  //获取订单有关司机和车辆安排
  getdriverandcar: function () {
    var that = this
    that.setData({
      driverlist: [],
    });
    console.log(that.data.driverlist.length)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/DriverPerson/GetDriverCarInfo.ashx',
      data: {
        IDD: that.data.IDD,
      },
      method: 'GET',
      success: function (res) {

        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          that.setData({
            loadError: true
          });
          return
        }

        var data = tool.getRegularJson(res.data);
        console.log(data)

        if (data.data.length > 0) {

          if (data.data[0].bcTime != '') {
            for (var i = 0; i < data.data.length; i++) {
              data.data[i].bcTime = data.data[i].bcTime.substring(5, 16)
            }
          }

          that.setData({
            driverlist: data.data
          });

          console.log(that.data.driverlist)
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  },

  loadOrderData: function () {
    var that = this
    var userInfo = wx.getStorageSync('userInfo');

    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/OrderInfo.ashx',
      data: {
        IDD: that.data.IDD,
        methd: '2',
        Tel: userInfo.cTel
      },
      method: 'GET',
      success: function (res) {
        // success

        var data = tool.getRegularJson(res.data);
        var code = data.code;
        console.log(res);
        if (code == 0) {
          that.setData({
            list: data.data[0],
          });
          console.log(that.data.list)
          console.log(that.data.list.length)

        } else {

        }
      },
      fail: function () {

      },
      complete: function () {
      }
    })
  },

  cancel: function (e) {
    var that = this
    console.log(e)
    var bctype = e.currentTarget.dataset.bctype
    var bcname = ''
    var ldnum = ''
    switch (bctype) {
      case '0':
        bcname = e.currentTarget.dataset.bcname
        ldnum = e.currentTarget.dataset.ldnum
        break;
      case '1':
        bcname = e.currentTarget.dataset.carname
        break;
      case '2':
        bcname = that.data.IDD
        break;
    }
    var userInfo = wx.getStorageSync('newuserInfo');
    console.log(userInfo)
    wx.request({
      url: 'https://www.lanshuicar.com/Server/CustS/LinesAtnto.ashx',
      data: {
        tel: userInfo.cTel,
        pwd: userInfo.UserPwd,
        act: 'atn_cancel_more',
        idd: that.data.IDD,
        ldnum: ldnum,
        bcname: bcname,
        bctype: bctype
      },
      method: 'GET',
      success: function (res) {
        if (res.statusCode == 404) {
          //接口加载失败
          console.log('加载接口错误');
          //显示错误信息
          that.showError('订单信息接口请求失败')
          return
        }
        var data = tool.getRegularJson(res.data);
        console.log(data)
        if (data.code == '0') {
          wx.showToast({
            title: '取消成功',
            icon: 'succes',
            duration: 2000,
            mask: true
          })

          that.loadData()
          that.loadOrderData()
          that.newGetDriverAndCar()
          // that.getdriverandcar()
        } else {
          wx.showToast({
            title: '取消失败，请重试',
            icon: 'none',
            duration: 2000,
            mask: true
          })
        }
      },
      fail: function () {
        console.log('加载错误');
      }
    })
  }
})